package top.hmtools.manager.asynchronous;

import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import top.hmtools.RLContext;

/**
 * 基于观察者模式的监听者。 <br>
 * 保证执行 用户请求日志缓存队列 数据写入数据库的 守护线程挂了之后能够自动重启。 <br>
 * 使用了线程池
 * @author HyboJ
 *
 */
public class ThreadBossObserver  implements Observer{
	protected final Logger logger = LoggerFactory.getLogger(ThreadBossObserver.class);

	
	private RLContext rlContext;

	public ThreadBossObserver(RLContext rlContext) {
		this.rlContext = rlContext;
	}

	@Override
	public void update(Observable o, Object arg) {
		try {
			logger.debug("检测到ThreadBoss线程发生异常，现尝试重启。。。");
			ThreadBoss threadBoss = new ThreadBoss(this.rlContext);
			threadBoss.addObserver(this);
			Thread thread = new Thread(threadBoss);
			thread.setDaemon(true);
			thread.setName("threadBoss-"+new Date());
			thread.start();
			logger.debug("ThreadBoss线程重启成功。。。");
		} catch (Exception e) {
			logger.error("ThreadBoss线程重启失败："+e.getMessage(),e);
		}
	}

}
